/**
 * 
 */
package com.module.hq.view;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import com.dalonedrau.jogl.engine.Action;
import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.InputEvent;
import com.dalonedrau.jogl.engine.JOGLErrorHandler;
import com.dalonedrau.jogl.opengl.View;
import com.dalonedrau.jogl.opengl.ui.Button;
import com.dalonedrau.jogl.opengl.ui.Label;
import com.dalonedrau.jogl.opengl.ui.UIFactory;
import com.dalonedrau.jogl.opengl.ui.panel.PagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.jogl.opengl.ui.panel.PortraitPagePanel;
import com.dalonedrau.jogl.util.UtilityMethods;
import com.module.hq.engine.HQController;
import com.module.hq.ui.mono.borders.HQMonoPanelBorder;

/**
 * @author Donald
 */
public final class DungeonSplashView implements View {
	/**
	 * the one and only instance of the <code>DungeonSplashView</code> class.
	 */
	private static DungeonSplashView	instance;
	/**
	 * Gives access to the singleton instance of {@link DungeonSplashView}.
	 * @return {@link DungeonSplashView}
	 */
	public static DungeonSplashView getInstance() {
		if (DungeonSplashView.instance == null) {
			DungeonSplashView.instance = new DungeonSplashView();
		}
		return DungeonSplashView.instance;
	}
	private Button		btnOkay;
	private Label		label;
	/** the panel used to organize items. */
	private PagePanel	panel;
	/** flag indicating initialization has been completed. */
	private boolean		runInit	= false;
	/** the splash text. */
	private String		text;
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleKeyEvent(final KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_ESCAPE:
			System.out.println("esc");
			GameApplet.getInstance().exit();
			break;
		case KeyEvent.VK_UP:
			break;
		case KeyEvent.VK_DOWN:
			break;
		case KeyEvent.VK_ENTER:
			break;
		case KeyEvent.VK_TAB:
			break;
		case KeyEvent.VK_BACK_SPACE:
			try {} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
			break;
		case KeyEvent.VK_SPACE:
			try {
				// textfield.addCharacter(e.getKeyChar());
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
			break;
		default:
			if (Character.isLetterOrDigit(e.getKeyChar())) {
				try {
					// textfield.addCharacter(e.getKeyChar());
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
			break;
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMouseOver(final float x, final float y) {
		panel.handleMouseOver(x, y);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMousePress(final MouseEvent e, final float x,
			final float y) {
		panel.handleMouseClick(e, x, y);
	}
	/**
	 * Initializes the {@link View}. Choose your character bar dwa ch explainer
	 * elf sor begin
	 * @throws Exception if an error occurs
	 */
	public void init() throws Exception {
		panel = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
		// 1st row - text
		label = UIFactory.getInstance().getStandardH1Label(text);
		label.setMaximumWidth(550);
		label.setBorder(UIFactory.getInstance().getVXStyleDialogBorder());
		panel.addChild(label);
		// 2nd row - button
		panel.addChild(UIFactory.getInstance().getSpacer(1, 10));
		initOkayButton();
		panel.addChild(btnOkay);
		Point pt = UtilityMethods.getInstance().centerComponentInFrame(
				panel);
		panel.setPosition(pt.x, pt.y, 0.0f);
		panel.setVisible(true);
		runInit = true;
	}
	/** Initializes the okay button. */
	private void initOkayButton() {
		btnOkay = UIFactory.getInstance().getStandardButton("Okay");
		btnOkay.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				try {
					// add call to load character chooser screen to input events
					InputEvent.getInstance().addAction(
							HQController.getInstance(), // object
							HQController.class.getMethod(
									"loadDungeonScreen", null),
							null
							);
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
		});
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		if (!runInit) {
			try {
				init();
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
		}
		panel.prepareForRendering();
	}
	/**
	 * Sets the text.
	 * @param val the text to set
	 */
	public void setText(final String val) {
		text = val;
		if (label != null) {
			label.setText(text);
		}
	}
}
